// import { engine } from "../../engine/engine";
import { IAudioEngine } from "./IAudioEngine";
import { LocalManager } from "../data/LocalManager";
import { IValue } from "../data/IValue";
export class MusicManager {

    protected tempName: string;


    protected audioEngine: IAudioEngine;
    private musicVolume: IValue;
    private musicFlag: IValue;
    constructor(projectName: string, audioEngine: IAudioEngine,localManager: LocalManager) {
        this.audioEngine = audioEngine;
        this.musicFlag = localManager.getLocalValue(projectName + 'musicFlag', 1);
        this.musicVolume = localManager.getLocalValue(projectName + 'musicVolume', 1);
    }
    getMusicFlag() {
        if (!this.musicFlag) {
            return false;
        }
        let num = this.musicFlag.getValue();
        return num == 1 ? true : false;
    }
    getMaxNum() {
        return this.audioEngine.getMaxAudioInstance();
    }
    getVolume() {
        if (!this.musicVolume) {
            return 1;
        }
        return this.musicVolume.getValue();
    }
    play(name: string, res: any, isLoop: boolean) {
        if (this.tempName == name) {
            return;
        }
        this.stop();
        this.tempName = name;
        let audioId = this.audioEngine.playMusic(res, isLoop);
        if (!isLoop) {
            this.audioEngine.setFinishCallback(audioId, this.stop.bind(this, name));
        }
    }

    pause() {
        this.audioEngine.pauseMusic();
    }

    resume() {
        this.audioEngine.resumeMusic();
    }

    stop() {

        if (!this.tempName) {
            return;
        }

        this.audioEngine.stopMusic();
        this.tempName = null;

    }
    setMusicFlag(flag: boolean) {
        if (!this.musicFlag) {
            return;
        }
        let num = flag ? 1 : 0;
        this.musicFlag.setValue(num);

    }

    setVolume(count: number) {
        if (!this.musicVolume) {
            return;
        }
        this.musicVolume.setValue(count);
        this.audioEngine.setMusicVolume(count);

    }

    isPlaying() {
        return this.audioEngine.isMusicPlaying();
    }

    clear() {
        this.stop()
    }

}